package com.zjff.mobiletwo.service.util;

import com.zjff.mobiletwo.entity.UserEntity;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class CreateJwt {
    public static String getoken(UserEntity userEntity){
        //Jwts.builder()生成
        //Jwts.builder()验证
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId(userEntity.getId()+"")
                .setSubject(userEntity.getUsername())//用户名
                .setIssuedAt(new Date())//登录时间
                .signWith(SignatureAlgorithm.HS256, "key").setExpiration(new Date(new
                        Date().getTime()+86400000));
        //设置过期时间
        //前三个为载荷playload最后一个为头部header
        System.out.println(jwtBuilder.compact());
        return jwtBuilder.compact();
    }
}
